home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MPW C++ / MPW C++ 3.1 / Interfaces / CIncludes / iostream.h < prev    next >
Text File  |  1990-09-11  |  14KB  |  577 lines

  1. /*ident    "@(#)ctrans:incl/iostream.h    1.1.7.9" */
  2. /**************************************************************************
  3.                         Copyright (c) 1984 AT&T
  4.                           All Rights Reserved   
  5.  
  6.         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
  7.       
  8.         The copyright notice above does not evidence any        
  9.         actual or intended publication of such source code.
  10.  
  11. *****************************************************************************/
  12. #ifndef __IOSTREAM__
  13. #define __IOSTREAM__
  14.  
  15. // #include <memory.h>
  16. #include <String.h> // for Macintosh
  17.         /* Some inlines use memcpy */
  18.  
  19. /* If EOF is defined already verify that it is -1.  Otherwise
  20.  * define it.
  21.  */
  22.  
  23. #ifdef EOF
  24. #    if EOF!=-1
  25. #        define EOF (-1) 
  26. #    endif
  27. #else
  28. #    define EOF (-1)
  29. #endif
  30.  
  31. /* Don't worry about NULL not being 0 */
  32. #ifndef NULL
  33. #    define NULL 0
  34. #endif
  35.  
  36. #define    zapeof(c) ((c)&0377)
  37.         /* extracts char from c. The critical requirement is
  38.          *      zapeof(EOF)!=EOF
  39.          * ((c)&0377) and ((unsigned char)(c)) are alternative definitions
  40.          * whose efficiency depends on compiler environment.
  41.          */
  42.  
  43. typedef long streampos ;
  44. typedef long streamoff ;
  45.  
  46. class streambuf ;
  47. class ostream ;
  48.  
  49.  
  50. class ios {
  51. public: /* Some enums are declared in ios to avoid pollution of
  52.      * global namespace
  53.      */
  54.     enum io_state    { goodbit=0, eofbit=1, failbit=2, badbit=4,
  55.                 hardfail=0200};
  56.                 /* hard fail can be set and reset internally,
  57.                  * but not via public function */
  58.     enum open_mode    { in=1, out=2, ate=4, app=010, trunc=020,
  59.                 nocreate=040, noreplace=0100} ;
  60.     enum seek_dir    { beg=0, cur=1, end=2 } ;
  61.  
  62.     /* flags for controlling format */
  63.     enum        { skipws=01,    
  64.                     /* skip whitespace on input */
  65.               left=02,  right=04, internal=010,
  66.                     /* padding location */
  67.               dec=020, oct=040, hex=0100, 
  68.                     /* conversion base */
  69.               showbase=0200, showpoint=0400, uppercase=01000,
  70.               showpos=02000, 
  71.                     /* modifiers */
  72.               scientific=04000, fixed=010000,
  73.                     /* floating point notation */
  74.               unitbuf=020000, stdio=040000
  75.                     /* stuff to control flushing */
  76.               } ;
  77.     static const long 
  78.             basefield ; /* dec|oct|hex */
  79.     static const long
  80.             adjustfield ; /* left|right|internal */
  81.     static const long
  82.             floatfield ; /* scientific|fixed */
  83. public:
  84.             ios(streambuf*) ;
  85.     virtual        ~ios() ;
  86.  
  87.     long        flags()     { return x_flags ; }
  88.     long        flags(long f);
  89.  
  90.     long        setf(long setbits, long field);
  91.     long        setf(long) ;
  92.     long        unsetf(long) ;
  93.  
  94.     int        width()        { return x_width ; }
  95.     int        width(int w)
  96.     {
  97.             int i = x_width ; x_width = w ; return i ;
  98.     }
  99.         
  100.     ostream*    tie(ostream* s); 
  101.     ostream*    tie()        { return x_tie ; }
  102.     char        fill(char) ;
  103.     char        fill()         { return x_fill ; }
  104.     int        precision(int) ;
  105.     int        precision()    { return x_precision ; }
  106.  
  107.     int        rdstate()    { return state ; }
  108.             operator void*()
  109.                 {
  110.                 if (state&(failbit|badbit|hardfail)) return 0 ;
  111.                 else return this ;
  112.                 }
  113.  
  114.     int        operator!()
  115.                 { return state&(failbit|badbit|hardfail); } 
  116.     int        eof()    { return state&eofbit; }
  117.     int        fail()    { return state&(failbit|badbit|hardfail); }
  118.     int        bad()    { return state&badbit ; }
  119.     int        good()    { return state==0 ; }
  120.     void        clear(int i =0) 
  121.                 {    
  122.                 state =  (i&0377) | (state&hardfail) ;
  123.                 ispecial = (ispecial&~0377) | state ; 
  124.                 ospecial = (ospecial&~0377) | state ; 
  125.                 }
  126.     streambuf*    rdbuf() { return bp ;} 
  127.  
  128. public: /* Members related to user allocated bits and words */
  129.     long &        iword(int) ;
  130.     void* &        pword(int) ;
  131.     static long    bitalloc() ;
  132.     static int    xalloc() ;
  133.  
  134. private: /*** privates for implemting allocated bits and words */ 
  135.     static long    nextbit ;
  136.     static long    nextword ;
  137.     
  138.     int        nuser ;
  139.     union ios_user_union*
  140.             x_user ;
  141.     void    uresize(int) ;
  142. public: /* static member functions */
  143.     static void    sync_with_stdio() ;
  144.  
  145. protected:
  146.     enum         { skipping=01000, tied=02000 } ;
  147.             /*** bits 0377 are reserved for userbits ***/
  148.     streambuf*    bp;
  149.     void        setstate(int b)
  150.             {    state |= (b&0377) ;
  151.                 ispecial |= b&~skipping ;
  152.                 ispecial |= b ;
  153.             }
  154.     int        state;    
  155.     int        ispecial;        
  156.     int        ospecial;
  157.     int        isfx_special;
  158.     int        osfx_special;        
  159.     int        delbuf;
  160.     ostream*    x_tie;
  161.     long         x_flags;
  162.     short        x_precision;
  163.     char        x_fill;
  164.     short         x_width;
  165.     
  166.     static void    (*stdioflush)() ;
  167.  
  168.     void        init(streambuf*) ;
  169.                 /* Does the real work of a constructor */
  170.             ios() ; /* No initialization at all. Needed by
  171.                  * multiple inheritance versions */
  172.     int        assign_private ;
  173.                 /* needed by with_assgn classes */
  174. private:        
  175.             ios(ios&) ; /* Declared but not defined */
  176.     void        operator=(ios&) ; /* Declared but not defined */
  177. public:   /* old stream package compatibility */
  178.     int        skip(int i) ; 
  179. };
  180.  
  181. class streambuf {
  182.     short        alloc;    
  183.     short        x_unbuf;
  184.     char*         x_base;    
  185.     char*        x_pbase;
  186.     char*        x_pptr;    
  187.     char*         x_epptr;
  188.     char*         x_gptr;
  189.     char*        x_egptr;
  190.     char*        x_eback;
  191.     int        x_blen;    
  192.     private:
  193.             streambuf(streambuf&); /* Declared but not defined */
  194.     void        operator=(streambuf&); /* Declared but not defined */
  195.     public:
  196.     void        dbp();
  197.     protected:
  198.     char*        base()         { return x_base ; }
  199.     char*        pbase()        { return x_pbase ; }
  200.     char*        pptr()         { return x_pptr ; }
  201.     char*        epptr()     { return x_epptr ; }
  202.     char*        gptr()         { return x_gptr ; }
  203.     char*        egptr()     { return x_egptr ; }
  204.     char*        eback()     { return x_eback ; }
  205.     char*         ebuf()        { return x_base+x_blen ; }
  206.     int        blen()        { return x_blen; }
  207.     void        setp(char*  p, char*  ep)
  208.     {
  209.         x_pbase=x_pptr=p ; x_epptr=ep ;
  210.     }
  211.     void        setg(char*  eb,char*  g, char*  eg)
  212.     {
  213.         x_eback=eb; x_gptr=g ; x_egptr=eg ;
  214.     }
  215.     void        pbump(int n) 
  216.     { 
  217.         x_pptr+=n ;
  218.     }
  219.  
  220.     void        gbump(int n) 
  221.     { 
  222.         x_gptr+=n ;
  223.         }
  224.  
  225.     void        setb(char* b, char* eb, int a = 0 )
  226.     {
  227.         if ( alloc && x_base ) delete x_base ;
  228.         x_base = b ;
  229.         x_blen= (eb>b) ? (eb-b) : 0 ;
  230.         alloc = a ;
  231.         }
  232.     int        unbuffered() { return x_unbuf; }
  233.     void        unbuffered(int unb) { x_unbuf = (unb!=0)  ; }
  234.     int        allocate()
  235.     {
  236.         if ( x_base== 0 && !unbuffered() ) return doallocate() ;
  237.         else                   return 0 ;
  238.     }
  239.     virtual int     doallocate();
  240.     public : 
  241.     virtual int    overflow(int c=EOF);
  242.     virtual int    underflow();
  243.     virtual int    pbackfail(int c);
  244.     virtual int    sync();
  245.     virtual streampos
  246.             seekoff(streamoff,seek_dir,int =ios::in|ios::out);
  247.     virtual streampos
  248.             seekpos(streampos, int =ios::in|ios::out) ;
  249.     virtual int    xsputn(const char*  s,int n);
  250.     virtual int    xsgetn(char*  s,int n);
  251.  
  252.     int        in_avail()
  253.     {
  254.         return x_gptr<x_egptr ? x_egptr-x_gptr : 0 ;
  255.     }
  256.  
  257.     int        out_waiting() 
  258.     {    
  259.         if ( x_pptr ) return x_pptr-x_pbase ;
  260.         else          return 0 ; 
  261.     }
  262.  
  263.     int        sgetc()
  264.     {
  265.         /***WARNING: sgetc does not bump the pointer ***/
  266.         return (x_gptr>=x_egptr) ? underflow() : zapeof(*x_gptr);
  267.     }
  268.     int        snextc()
  269.     {
  270.         return (++x_gptr>=x_egptr)
  271.                 ? x_snextc()
  272.                 : zapeof(*x_gptr);
  273.     }
  274.     int        sbumpc()
  275.     {
  276.         return  ( x_gptr>=x_egptr && underflow()==EOF ) 
  277.                 ? EOF 
  278.                 : zapeof(*x_gptr++) ;
  279.     }
  280.     void        stossc()
  281.     {
  282.         if ( x_gptr++ > x_egptr ) underflow() ;
  283.     }
  284.  
  285.     int        sputbackc(char c)
  286.     {
  287.         if (x_gptr > x_eback ) {
  288.             if ( *--x_gptr == c ) return zapeof(c) ;
  289.             else               return zapeof(*x_gptr=c) ;
  290.         } else {
  291.             return pbackfail(c) ;
  292.         }
  293.     }
  294.  
  295.     int        sputc(int c)
  296.     {
  297.         return (x_pptr>=x_epptr) ? overflow(zapeof(c))
  298.                       : zapeof(*x_pptr++=c);
  299.     }
  300.     int        sputn(const char*  s,int n)
  301.      {
  302.         if ( n <= (x_epptr-x_pptr) ) {
  303.             memcpy(x_pptr,s,n) ;
  304.             pbump(n);
  305.             return n ;
  306.         } else {
  307.             return xsputn(s,n) ;
  308.         }
  309.     }
  310.     int        sgetn(char*  s,int n)
  311.     {
  312.         if ( n <= (x_egptr-x_gptr) ) {
  313.             memcpy(s,x_gptr,n) ;
  314.             gbump(n);
  315.             return n ;
  316.         } else {
  317.             return xsgetn(s,n) ;
  318.         }
  319.     }
  320.     virtual streambuf*
  321.             setbuf(char*  p, int len) ;
  322.        streambuf*    setbuf(unsigned char*  p, int len) ;
  323.  
  324.     streambuf*    setbuf(char*  p, int len, int count) ;
  325.                 /* obsolete third argument */
  326.               /*** Constructors -- should be protected ***/
  327.             streambuf() ;
  328.             streambuf(char*  p, int l) ;
  329.  
  330.             streambuf(char*  p, int l,int c) ;
  331.             /* 3 argument form is obsolete.
  332.              * Use strstreambuf.
  333.              */
  334.     virtual        ~streambuf() ;
  335. private:
  336.     int        x_snextc() ;
  337. };
  338.  
  339. class istream : virtual public ios {
  340. public: /* Constructor */
  341.             istream(streambuf*) ;
  342.     virtual        ~istream() ;
  343. public:    
  344.     int        ipfx(int noskipws=0)
  345.             {    if ( noskipws?(ispecial&~skipping):ispecial) {
  346.                     return do_ipfx(noskipws) ;
  347.                 } else return 1 ;
  348.             }
  349.     void        isfx() { } 
  350.     istream&    seekg(streampos p) ;
  351.     istream&    seekg(streamoff o, seek_dir d) ;
  352.        streampos    tellg() ; 
  353.     istream&    operator>> (istream& (*f)(istream&))
  354.             {    return (*f)(*this) ; }
  355.     istream&    operator>> (ios& (*f)(ios&) ) ;
  356.     istream&    operator>>(char*);
  357.     istream&    operator>>(unsigned char*);
  358.     istream&    operator>>(unsigned char& c)
  359.             {    if ( ipfx(0) ) {
  360.                     if (  bp->in_avail() ) {
  361.                         c = bp->sbumpc() ;
  362.                     } else  xget((char*)&c) ;
  363.                 }
  364.                 return *this ;
  365.             }
  366.     istream&    operator>>(char& c)
  367.             {    if ( ipfx(0) ) {
  368.                     if (  bp->in_avail() ) {
  369.                         c = bp->sbumpc() ;
  370.                     } else  xget((char*)&c) ;
  371.                 }
  372.                 return *this ;
  373.             }
  374.     istream&    operator>>(short&);
  375.     istream&    operator>>(int&);
  376.     istream&    operator>>(long&);
  377.     istream&    operator>>(unsigned short&);
  378.     istream&    operator>>(unsigned int&);
  379.     istream&    operator>>(unsigned long&);
  380.     istream&    operator>>(float&);
  381.     istream&    operator>>(double&);
  382.     istream&    operator>>(extended&); // for Macintosh, add extended support
  383.     istream&    operator>>(comp&);     // for Macintosh, add comp support
  384.     istream&    operator>>(streambuf*);
  385.     istream&    get(char* , int lim, char delim='\n');
  386.     istream&    get(unsigned char* b,int lim, char delim='\n');
  387.     istream&    getline(char* b, int lim, char delim='\n');
  388.     istream&    getline(unsigned char* b, int lim, char delim='\n');
  389.     istream&    get(streambuf& sb, char delim ='\n');
  390.     istream&    get(unsigned char& c)
  391.             {
  392.                 if ( ipfx(1) && bp->in_avail()) {
  393.                     x_gcount = 1 ;
  394.                     c = bp->sbumpc() ;
  395.                 } else {
  396.                     xget((char*)&c) ;
  397.                 }
  398.                 return *this ;
  399.             }
  400.     istream&    get(char& c)
  401.             {
  402.                 if ( ipfx(1) && bp->in_avail()) {
  403.                     x_gcount = 1 ;
  404.                     c = bp->sbumpc() ;
  405.                 } else {
  406.                     xget(&c) ;
  407.                 }
  408.                 return *this ;
  409.             }
  410.     int         get()
  411.             {
  412.                 int c ;
  413.                 if ( !ipfx(1) ) return EOF ;
  414.                 else {
  415.                     c = bp->sbumpc() ;
  416.                     if ( c == EOF ) setstate(eofbit) ;
  417.                     return c ;
  418.                     }
  419.             }
  420.     int        peek() 
  421.             {
  422.                 if ( ipfx(-1) ) return bp->sgetc() ;
  423.                 else        return EOF ;
  424.  
  425.             }
  426.     istream&    ignore(int n=1,int delim=EOF) ;
  427.     istream&    read(char*  s,int n);
  428.     istream&    read(unsigned char* s,int n) 
  429.             {
  430.                 return read((char*)s,n) ;
  431.             }
  432.     int        gcount() ;
  433.     istream&    putback(char c);
  434.     int        sync()    { return bp->sync() ; }
  435. protected:  
  436.     int        do_ipfx(int noskipws) ;
  437.     void        eatwhite() ;
  438.             istream() ;
  439. private: 
  440.     int        x_gcount ;
  441.     void         xget(char*  c) ;
  442. public: /*** Obsolete constructors, carried over from stream package ***/
  443.             istream(streambuf*, int sk, ostream* t=0) ;
  444.                 /* obsolete, set sk and tie
  445.                  * via format state variables */
  446.             istream(int size ,char*,int sk=1) ;
  447.                 /* obsolete, use strstream */
  448.             istream(int fd,int sk=1, ostream* t=0) ;
  449.                 /* obsolete use fstream */
  450. };
  451.  
  452. class ostream : virtual public ios {
  453. public: /* Constructor */
  454.             ostream(streambuf*) ;
  455.     virtual        ~ostream();
  456. public:    
  457.     int        opfx()    /* Output prefix */
  458.             {    if ( ospecial )    return do_opfx() ;
  459.                 else        return 1 ;
  460.             }
  461.     void        osfx()
  462.             {    if ( osfx_special ) do_osfx() ; }
  463.             
  464.     ostream&    flush() ;
  465.     ostream&    seekp(streampos p) ;
  466.     ostream&    seekp(streamoff o, seek_dir d) ;
  467.      streampos    tellp() ; 
  468.     ostream&    put(char c) ;
  469.     ostream&    operator<<(char c) { put(c) ; osfx() ; return *this ; }
  470.  
  471.     ostream&    operator<<(unsigned char c) 
  472.     {    put(c) ; osfx() ; return *this ; }
  473.  
  474.     ostream&    operator<<(const char*);
  475.     ostream&    operator<<(int a); 
  476.     ostream&    operator<<(long);
  477.     ostream&    operator<<(float d) { return (*this)<<(extended) d; } // for Macintos    
  478.     ostream&    operator<<(double d) { return (*this)<<(extended) d; } // for Macintosh
  479.     ostream&    operator<<(extended); // for Macintosh
  480.     ostream&    operator<<(unsigned int a);
  481.     ostream&    operator<<(unsigned long);
  482.     ostream&    operator<<(void*);
  483.     ostream&    operator<<(streambuf*);
  484.     ostream&    operator<<(short i) { return *this << (int)i ; }
  485.     ostream&    operator<<(unsigned short i) 
  486.             { return *this << (int)i  ; }
  487.  
  488.     ostream&    operator<< (ostream& (*f)(ostream&))
  489.             { return (*f)(*this) ; }
  490.     ostream&    operator<< (ios& (*f)(ios&) ) ;
  491.  
  492.     ostream&    write(const char*  s,int n)    
  493.     {
  494.         if ( !state ) {
  495.             if ( bp->sputn(s,n) != n ) setstate(eofbit|failbit);
  496.             }
  497.         return *this ;
  498.     }
  499.     ostream&    write(const unsigned char* s, int n)
  500.     {
  501.         return write((const char*)s,n);
  502.     }
  503. protected: /* More ostream members */
  504.     int        do_opfx() ;
  505.     void    do_osfx() ;
  506.             ostream() ;
  507.  
  508. public: /*** Obsolete constructors, carried over from stream package ***/
  509.             ostream(int fd) ;
  510.                 /* obsolete use fstream */
  511.             ostream(int size ,char*) ;
  512.                 /* obsolete, use strstream */
  513. } ;
  514.  
  515. class iostream : public istream, public ostream {
  516. public:
  517.             iostream(streambuf*) ;
  518.     virtual        ~iostream() ;
  519. protected:
  520.             iostream() ;
  521.     } ;
  522.  
  523. class Iostream_init;  // for Macintosh
  524.  
  525. class istream_withassign : public istream {
  526. public:
  527.             istream_withassign() ;
  528.             istream_withassign(Iostream_init*); // for Macintosh
  529.     virtual        ~istream_withassign() ;
  530.     istream_withassign&    operator=(istream&) ;
  531.     istream_withassign&    operator=(streambuf*) ;
  532.     
  533. } ;
  534.  
  535. class ostream_withassign : public ostream {
  536. public:
  537.             ostream_withassign() ;
  538.             ostream_withassign(Iostream_init*); // for Macintosh                        
  539.     virtual        ~ostream_withassign() ;
  540.     ostream_withassign&    operator=(ostream&) ;
  541.     ostream_withassign&    operator=(streambuf*) ;
  542. } ;
  543.  
  544. class iostream_withassign : public iostream {
  545. public:
  546.             iostream_withassign() ;
  547.     virtual        ~iostream_withassign() ;
  548.     iostream_withassign&    operator=(ios&) ;
  549.     iostream_withassign&    operator=(streambuf*) ;
  550. } ;
  551.  
  552. extern istream_withassign cin ;
  553. extern ostream_withassign cout ;
  554. extern ostream_withassign cerr ;
  555. extern ostream_withassign cdebug ;
  556.  
  557. ios&        dec(ios&) ; 
  558. ostream&    endl(ostream& i) ;
  559. ostream&    ends(ostream& i) ;
  560. ostream&    flush(ostream&) ;
  561. ios&        hex(ios&) ;
  562. ios&        oct(ios&) ; 
  563. istream&    ws(istream&) ;
  564.  
  565. class Iostream_init { // for Macintosh: This used to define
  566.                       // a static instance; it is now defined
  567.                       // in cstreams.c.
  568.     static int    stdstatus ; /* see cstreams.c */
  569.     // static int    initcount ; deleted for Macintosh
  570.     friend        ios ;
  571. public:
  572.     Iostream_init() ; 
  573.     ~Iostream_init() ; 
  574. }; // iostream_init ; deleted for Macintosh    
  575.  
  576. #endif
  577.